/*
 *******************************************************************************
 * All rights Reserved, Copyright (C) www.gm-sz.com 2013
 * FileName: common.js
 * Modify record:
 * NO. |     Date       |    Version      |    Name         |      Content
 * 1   | 2013-11-04     |       1.0       |  GMSZ)GuoRuofei | original version
 *******************************************************************************
 */

/**
 * 常用工具组件
 * 
 * @require jquery.js
 */
var commonutil = commonutil || {};

commonutil.path = '';
commonutil.actionPath = '';
commonutil.pagePath = '';
commonutil.SUCCESS_FLAG = 200;
commonutil.cardUrl = 'http://localhost:8887';

/**
 * Description: Ajax操作
 * 
 * @param requrl
 * @param datas
 * @param successFun
 * @param repType
 */
commonutil.doAjax = function(requrl, datas, successFun, repType) {
	$.ajax({
		url : requrl,
		data : datas,
		success : successFun,
		dataType : repType,
		type : 'POST',
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			if (XMLHttpRequest.status == 401) {
				window.location = commonutil.path + '/page/errorcode/401.jsp';
			} else if (XMLHttpRequest.status == 403) {
				window.location = commonutil.path + '/page/errorcode/403.jsp';
			} else if (XMLHttpRequest.status == 404) {
				window.location = commonutil.path + '/page/errorcode/404.jsp';
			} else {
				window.location = commonutil.path + '/page/errorcode/500.jsp';
			}
		}
	});
};

/**
 * Description: 去前后空格
 * @param str
 * @returns
 */
commonutil.stringTrim = function(str) {
	return str.replace(/(^\s*)|(\s*$)/g, "");
};

/**
 * Description: 长度交验
 * @param str
 * @param length
 * @returns {Boolean}
 */
commonutil.checkLength = function(str, length){
	if(str.length <= length){
		return true;
	}else{
		return false;
	}
};

/**
 * Description: 数组是否包含指定值
 * @param {Array} arr
 * @param {String|Number} value
 * @returns {Boolean}
 */
commonutil.arrayContain = function(arr, value) {
	for (var i = 0; i < arr.length; i++) {
		if (arr[i] == value) {
			return true;
		}
	}
	return false;
};

/**
 * Description: 建立websocket链接并返回实例，如果当前浏览器不支持websocket则返回null
 * @returns 
 */
commonutil.getWebSocket = function(subUrl) {
	var url = 'ws://' + location.host + commonutil.path +"/"+ subUrl;
	if ('WebSocket' in window)
		ws = new WebSocket(url);
	else if ('MozWebSocket' in window)
		ws = new MozWebSocket(url);
	else
		alert('当前浏览器不支持websocket，请使用IE10+、Chrome17+、Firefox7+');
	return ws;
};

/**
 * Description: 如果用户未登录则返回null
 * @returns 
 */
commonutil.getUserKey = function() {
	var userKey = null, allCookies = document.cookie;
	var pos = allCookies.indexOf('omuser=');
	if (pos != -1) {
		var start = pos + 7;
		var end = allCookies.indexOf(';', start);
		if (end == -1)
			end = allCookies.length;
		userKey = allCookies.substring(start, end);
	} else {
		alert('用户未登录');
	}
	return userKey;
};

/**
 * //判断正整数 含0
 * @param input
 * @returns
 */
commonutil.checkRate= function checkRate(Str) {   
     var re = /^[0-9]+[0-9]*]*$/;   
  
     if (!re.test(Str))   
    {     
        return false;   
     } 
     return true;
};
/**
 * 是否浮点数
 * @param oNum
 * @returns {Boolean}
 */
commonutil.isfloat=function (val){

	 if((typeof val) === 'undefined' || val === null || val === ''){
		return true;
	 } 
	 
	 var strP=/^\d+(\.\d+)?$/; 
	 var strPKeXue=/^((\d+.?\d+)[Ee]{1}[+]?(\d+))$/; 
	 
	 if(strP.test(val) || strPKeXue.test(val)) return true ;
	 
	 return false;

	}
/**
 * 必输
 * @param {Object} val
 */
commonutil.testRequire = function testRequire(val){
	if((typeof val) === 'undefined' || val === null || val === ''){
		return false;
	}
	return true;
};

commonutil.testMaxDBColumnSize = function(val, spec){
	if((typeof val) === 'undefined' || val === null || val === ''){
		return true;
	}
	var valChars = val.split("");
	var zhCNCharCount = 0;
	for(var i=0; i < valChars.length; i++){
		if(/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(valChars[i])){
			zhCNCharCount = zhCNCharCount + 1;
		}
	}
	
	// 汉字一个算两个长度
	var totalLength = val.length + zhCNCharCount;
	var specV = spec;
	if((typeof spec) === 'string'){
		if(/^-?\d+$/.test(spec)){
			specV = parseInt(spec);
		}
		else if(/^(-?\d+)(\.\d+)?$/.test(val)){
			specV = parseFloat(spec);
		}
	}
	if(totalLength > specV){
		return false;
	} else {
		return true;
	}
};
commonutil.testMinDBColumnSize = function(val, spec){
	var valChars = val.split("");
	var zhCNCharCount = 0;
	for(var i=0; i < valChars.length; i++){
		if(/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(valChars[i])){
			zhCNCharCount = zhCNCharCount + 1;
		}
	}
	
	// 汉字一个算两个长度
	var totalLength = val.length + zhCNCharCount;
	var specV = spec;
	if((typeof spec) === 'string'){
		if(/^-?\d+$/.test(spec)){
			specV = parseInt(spec);
		}
		else if(/^(-?\d+)(\.\d+)?$/.test(val)){
			specV = parseFloat(spec);
		}
	}
	if(totalLength < specV){
		return false;
	} else {
		return true;
	}
};

commonutil.testMaxCharLength = function(val, spec){
	if((typeof val) === 'undefined' || val === null || val === ''){
		return true;
	}
	var specV = spec;
	if((typeof spec) === 'string'){
		if(/^-?\d+$/.test(spec)){
			specV = parseInt(spec);
		}
		else if(/^(-?\d+)(\.\d+)?$/.test(val)){
			specV = parseFloat(spec);
		}
	}
	if(val.length > specV){
		return false;
	} else {
		return true;
	}
};

commonutil.testMinCharLength = function(val, spec){
	if((typeof val) === 'undefined' || val === null){
		return false;
	}
	var specV = spec;
	if((typeof spec) === 'string'){
		if(/^-?\d+$/.test(spec)){
			specV = parseInt(spec);
		}
		else if(/^(-?\d+)(\.\d+)?$/.test(val)){
			specV = parseFloat(spec);
		}
	}
	if(val.length < specV){
		return false;
	} else {
		return true;
	}
};

commonutil.testMax = function(val, spec){
	if((typeof val) === 'undefined' || val === null){
		return false;
	}
	if(isNaN(val)){
		return false;
	}
	var specV = spec;
	if((typeof spec) === 'string'){
		if(/^-?\d+$/.test(spec)){
			specV = parseInt(spec);
		}
		else if(/^(-?\d+)(\.\d+)?$/.test(val)){
			specV = parseFloat(spec);
		}
	}
	var valNum = val;
	if((typeof val) === 'string'){
		if(/^-?\d+$/.test(val)){
			valNum = parseInt(val);
		}
		else if(/^(-?\d+)(\.\d+)?$/.test(val)){
			valNum = parseFloat(val);
		}
	}
	if(valNum > specV){
		return false;
	} else {
		return true;
	}
};

commonutil.testMin = function(val, spec){
	if((typeof val) === 'undefined' || val === null){
		return false;
	}
	if(isNaN(val)){
		return false;
	}
	var valNum = val;
	var specV = spec;
	if((typeof spec) === 'string'){
		if(/^-?\d+$/.test(spec)){
			specV = parseInt(spec);
		}
		else if(/^(-?\d+)(\.\d+)?$/.test(val)){
			specV = parseFloat(spec);
		}
	}
	if((typeof val) === 'string'){
		if(/^-?\d+$/.test(val)){
			valNum = parseInt(val);
		}
		else if(/^(-?\d+)(\.\d+)?$/.test(val)){
			valNum = parseFloat(val);
		}
	}
	if(valNum < specV){
		return false;
	} else {
		return true;
	}
};

commonutil.testIntType = function (val){
	if((typeof val) === 'undefined' || val === null){
		return false;
	}
	if(isNaN(val)){
		return false;
	}
	var valStr = val
	if((typeof val) === 'number'){
		valStr = val + ""; 	
	}
	return /^-?\d+$/.test(val);
};

commonutil.testFloatType = function (val){
	if((typeof val) === 'undefined' || val === null){
		return false;
	}
	if(isNaN(val)){
		return false;
	}
	var valStr = val
	if((typeof val) === 'number'){
		valStr = val + ""; 	
	}
	return /^(-?\d+)(\.\d+)?$/.test(val);
};

commonutil.testNumType = function (val){
	if((typeof val) === 'undefined' || val === null){
		return ;
	}
	return !isNaN(val);
};

commonutil.testAlphabetAndNum = function (val){
	if(!isNaN(val)){
		return true;
	}
	return /^[A-Za-z0-9]+$/.test(val);
};

commonutil.testNonCN = function (val){
	if((typeof val) === 'undefined' || val === null){
		return true;
	}
	var valChars = val.split("");
	for(var i=0; i < valChars.length; i++){
		if(/[\u4E00-\u9FA5\uF900-\uFA2D]/.test(valChars[i])){
			return false;
		}
	}
	return true;
};

/**全选或反选*/
commonutil.selectAllCheck =function(PKCheckBoxName,obj) {
	var curr=$(obj).prop("checked");
	$("input[name='"+PKCheckBoxName+"']").prop("checked",curr);
		
};
/**选择一个或取消选择一个*/
commonutil.selectOneCheck=function(parentId,obj){
	var name=$(obj).prop("name");
	var currOne=$(obj).prop("checked");
	if(!currOne){
		$("#"+parentId).prop("checked",false);
	}else{
		 var check = $("input[name='"+name+"']");
		 var length=check.length;
		 var flag=true;
		 for(var i=0;i<length;i++ ){
			 var curr=$(check[i]).prop("checked");
			 if(!curr){
				 flag=false;
			 }
		 }
		 $("#"+parentId).prop("checked",flag);
	}
	
};

/***去除空格*/
commonutil.trim=function(obj){
	var val=$(obj).val();
	if(val){
		val=commonutil.stringTrim(val);
		$(obj).val(val);
	}
};

commonutil.GetUrlQueryStr=function(obj) {  
    var query = '?';  
    for(var param in obj){
    	var temp=obj[param];
    	if(temp!=null&&temp!=''){
        	var val=escape(temp);
        	if (val != null && val.toString().length > 0) {  
                query += param+'=' + val + '&';  
            }  
    	}
    }
    return query;  
};


